/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package slisfx.screens;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import slisfx.Globals;
import slisfx.exception.SLISExceptionFX;
import slisfx.resources.Load;
import slisfx.user.Userbase;
import slisfx.xml.XMLWriter;

/**
 *
 * @author Rebecca
 */
public class SetUserPermissionsFX extends Application implements Globals {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
    
    @Override
    public void start(final Stage primaryStage) {
        try {
            primaryStage.setTitle("Set User Permissions.");
            primaryStage.setResizable(false);

              Image image = new Image(new Load().loadFile("gray.png"));
              Image image1 = new Image(new Load().loadFile("NASA_IVV1_sm.png"));
              Image image2 = new Image(new Load().loadFile("WVU_LOGO_sm.png"));

              /*Creates our main Layout.
               * Sets the border to all black
               */
              BorderPane mainWindow = new BorderPane();
              mainWindow.setTop(new Rectangle(830,50, Color.BLACK));
              mainWindow.setBottom(new Rectangle(830,50, Color.BLACK));
              mainWindow.setLeft(new Rectangle(50,350, Color.BLACK));
              mainWindow.setRight(new Rectangle(50, 350, Color.BLACK));
              
              /* Main Menu 
               * Sets spacing.
               */
              VBox mainMenu = new VBox();
              mainMenu.setPadding(new Insets(10, 10, 10, 10));
              mainMenu.setSpacing(10);
              
              /* Horizantal Layout
               * Sets spacing
               */
              HBox imageBox = new HBox();
              imageBox.setPadding(new Insets(10, 10, 10, 200));
              imageBox.setSpacing(100);
              
              /* Stackpane
               * Sets background
               */
              StackPane stackPane = new StackPane();
              
              /* Title for Main Menu
               * Sets font style and size.
               */
              Text title = new Text("Please fill out the following form.");
              title.setFont(Font.font("Amble CN", FontWeight.BOLD, 14));
              title.setFill(Color.WHITE);
              mainMenu.getChildren().add(title);
              
              /* ImageView
               * Sets images
               */
              ImageView iv1BackGround = new ImageView();
              ImageView iv2 = new ImageView();
              ImageView iv3 = new ImageView();
              iv1BackGround.setImage(image);
              iv2.setImage(image1);
              iv3.setImage(image2);
              
                            
              imageBox.getChildren().addAll(iv2, iv3);
              
              mainMenu.getChildren().add(imageBox);
              
              /*Creating a textField*/
              final TextField uName = new TextField();
              uName.setPromptText("Enter user name.");
              uName.setPrefColumnCount(10);      
              mainMenu.getChildren().add(uName);
              
              /*Creating a textField for permissions*/
              final TextField perm = new TextField();
              perm.setPromptText("Modify users permission");
              perm.setPrefColumnCount(10);      
              mainMenu.getChildren().add(perm);
              
              Button modifyButton = new Button("Modify");
              modifyButton.setOnAction(new EventHandler<ActionEvent>()
              {
                  @Override
                  public void handle(ActionEvent event) {
                      if(Userbase.getUserbase().admin_check_user(uName.getText())){
                          Userbase.getUserbase().admin_modify_permissions(uName.getText(), perm.getText());
                          System.out.println(perm.getText());
                          System.out.println(uName.getText());
                          /*Re-Writes the xml document*/
                          new XMLWriter().writer();
                          new SystemViewFX().start(primaryStage);
                      }
                      else{
                          SLISExceptionFX except = new SLISExceptionFX("User does not exist.", SYSTEM_SCREEN);
                          except.start(primaryStage);  

                      }
                  }
              });
              
              Button returnButton = new Button("Return");
              returnButton.setOnAction(new EventHandler<ActionEvent>()
              {
                  @Override
                  public void handle(ActionEvent event) {
                      new SystemViewFX().start(primaryStage);
                  }
              });
              
              HBox buttonBox = new HBox();
              buttonBox.setPadding(new Insets(10, 10, 10, 275));
              buttonBox.setSpacing(50);
              buttonBox.getChildren().add(modifyButton);
              buttonBox.getChildren().add(returnButton);
              
              mainMenu.getChildren().add(buttonBox);
              

              /*We place it in the center of screen*/
              stackPane.getChildren().addAll(iv1BackGround, mainMenu);
              mainWindow.setCenter(stackPane);

              /*Sets the screen size and displays*/
              primaryStage.setScene(new Scene(mainWindow, WIDTH, HEIGHT));
              primaryStage.show();
        } catch (IOException ex) {
            Logger.getLogger(SetUserPermissionsFX.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
